package com.xge.component_caipu.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.xge.common_libary.bean.CaiPuListBean
import com.xge.common_libary.bean.CaiPuListInfoBean
import com.xge.common_libary.http.ApiResult
import com.xge.common_libary.http.RetrofitClient
import com.xge.common_libary.http.safeApiCall
import kotlinx.coroutines.launch

class CaiPuVM:ViewModel() {

    //菜谱列表
    var getCaiPuList = MutableLiveData<ApiResult<List<CaiPuListBean>>>()
    fun getCaiPuList() {
        viewModelScope.launch {
            val result = safeApiCall { RetrofitClient.apiService.getCaiPuList() }
            getCaiPuList.postValue(result)
        }
    }

    //菜谱列表
    var getCaiPuListInfo = MutableLiveData<ApiResult<CaiPuListInfoBean>>()
    fun getCaiPuListInfo(classid:Int?,page:Int) {
        viewModelScope.launch {
            val pageSize = 30
            val start = (page - 1) * pageSize
            val result = safeApiCall { RetrofitClient.apiService.getCaiPuListInfo(classid,start,"30") }
            getCaiPuListInfo.postValue(result)
        }
    }

    //菜谱列表
    var searchResult = MutableLiveData<ApiResult<CaiPuListInfoBean>>()
    fun searchList(keyword:String,page:Int) {
        viewModelScope.launch {
            val pageSize = 30
            val start = (page - 1) * pageSize
            val result = safeApiCall { RetrofitClient.apiService.searchCpList(keyword,start,"30") }
            getCaiPuListInfo.postValue(result)
        }
    }
}